home *** CD-ROM | disk | FTP | other *** search
/ LG Super CD / LG Super CD.iso / bitpim / bitpim-0.62-setup.exe / {app} / bitpim.exe / serial / serialposix.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2003-11-06  |  13.3 KB  |  383 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.3)
  3.  
  4. import sys
  5. import os
  6. import fcntl
  7. import termios
  8. import struct
  9. import select
  10. from serialutil import *
  11. VERSION = '$Revision: 1.18 $'.split()[1]
  12. if sys.hexversion < 33620208:
  13.     import TERMIOS
  14. else:
  15.     TERMIOS = termios
  16. if sys.hexversion < 33685744:
  17.     import FCNTL
  18. else:
  19.     FCNTL = fcntl
  20. plat = sys.platform.lower()
  21. if plat[:5] == 'linux':
  22.     
  23.     def device(port):
  24.         return '/dev/ttyS%d' % port
  25.  
  26. elif plat == 'cygwin':
  27.     
  28.     def device(port):
  29.         return '/dev/com%d' % (port + 1)
  30.  
  31. elif plat == 'openbsd3':
  32.     
  33.     def device(port):
  34.         return '/dev/ttyp%d' % port
  35.  
  36. elif plat[:3] == 'bsd' and plat[:6] == 'netbsd' and plat[:7] == 'freebsd' and plat[:7] == 'openbsd' or plat[:6] == 'darwin':
  37.     
  38.     def device(port):
  39.         return '/dev/cuaa%d' % port
  40.  
  41. elif plat[:4] == 'irix':
  42.     
  43.     def device(port):
  44.         return '/dev/ttyf%d' % port
  45.  
  46. elif plat[:2] == 'hp':
  47.     
  48.     def device(port):
  49.         return '/dev/tty%dp0' % (port + 1)
  50.  
  51. elif plat[:5] == 'sunos':
  52.     
  53.     def device(port):
  54.         return '/dev/tty%c' % (ord('a') + port)
  55.  
  56. else:
  57.     info = 'sys.platform = %r\nos.name = %r\nserialposix.py version = %s' % (sys.platform, os.name, VERSION)
  58.     print "send this information to the author of the pyserial:\n\n%s\n\nalso add the device name of the serial port and where the\ncounting starts for the first serial port.\ne.g. 'first serial port: /dev/ttyS0'\nand with a bit luck you can get this module running...\n"
  59.     
  60.     def device(portum):
  61.         return '/dev/ttyS%d' % portnum
  62.  
  63. baudEnumToInt = { }
  64. baudIntToEnum = { }
  65. for rate in (0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800, 9600, 19200, 38400, 57600, 115200, 230400, 460800, 500000, 576000, 921600, 1000000, 1152000, 1500000, 2000000, 2500000, 3000000, 3500000, 4000000):
  66.     
  67.     try:
  68.         i = eval('TERMIOS.B' + str(rate))
  69.         baudEnumToInt[i] = rate
  70.         baudIntToEnum[rate] = i
  71.     continue
  72.     continue
  73.  
  74.  
  75. if not hasattr(TERMIOS, 'TIOCMGET') and TERMIOS.TIOCMGET:
  76.     pass
  77. TIOCMGET = 21525
  78. if not hasattr(TERMIOS, 'TIOCMBIS') and TERMIOS.TIOCMBIS:
  79.     pass
  80. TIOCMBIS = 21526
  81. if not hasattr(TERMIOS, 'TIOCMBIC') and TERMIOS.TIOCMBIC:
  82.     pass
  83. TIOCMBIC = 21527
  84. if not hasattr(TERMIOS, 'TIOCMSET') and TERMIOS.TIOCMSET:
  85.     pass
  86. TIOCMSET = 21528
  87. if not hasattr(TERMIOS, 'TIOCM_DTR') and TERMIOS.TIOCM_DTR:
  88.     pass
  89. TIOCM_DTR = 2
  90. if not hasattr(TERMIOS, 'TIOCM_RTS') and TERMIOS.TIOCM_RTS:
  91.     pass
  92. TIOCM_RTS = 4
  93. if not hasattr(TERMIOS, 'TIOCM_CTS') and TERMIOS.TIOCM_CTS:
  94.     pass
  95. TIOCM_CTS = 32
  96. if not hasattr(TERMIOS, 'TIOCM_CAR') and TERMIOS.TIOCM_CAR:
  97.     pass
  98. TIOCM_CAR = 64
  99. if not hasattr(TERMIOS, 'TIOCM_RNG') and TERMIOS.TIOCM_RNG:
  100.     pass
  101. TIOCM_RNG = 128
  102. if not hasattr(TERMIOS, 'TIOCM_DSR') and TERMIOS.TIOCM_DSR:
  103.     pass
  104. TIOCM_DSR = 256
  105. if not hasattr(TERMIOS, 'TIOCM_CD') and TERMIOS.TIOCM_CD:
  106.     pass
  107. TIOCM_CD = TIOCM_CAR
  108. if not hasattr(TERMIOS, 'TIOCM_RI') and TERMIOS.TIOCM_RI:
  109.     pass
  110. TIOCM_RI = TIOCM_RNG
  111. if not hasattr(TERMIOS, 'FIONREAD') and TERMIOS.FIONREAD:
  112.     pass
  113. TIOCINQ = 21531
  114. TIOCM_zero_str = struct.pack('I', 0)
  115. TIOCM_RTS_str = struct.pack('I', TIOCM_RTS)
  116. TIOCM_DTR_str = struct.pack('I', TIOCM_DTR)
  117.  
  118. class Serial(SerialBase):
  119.     
  120.     def open(self):
  121.         if self._port is None:
  122.             raise SerialException('Port must be configured before it can be used.')
  123.         
  124.         self.fd = None
  125.         
  126.         try:
  127.             self.fd = os.open(self.portstr, os.O_RDWR | os.O_NOCTTY | os.O_NONBLOCK)
  128.         except Exception:
  129.             msg = None
  130.             self.fd = None
  131.             raise SerialException('Could not open port: %s' % msg)
  132.  
  133.         fcntl.fcntl(self.fd, FCNTL.F_SETFL, 0)
  134.         self._reconfigurePort()
  135.         self._isOpen = True
  136.  
  137.     
  138.     def _reconfigurePort(self):
  139.         if not (self.fd):
  140.             raise SerialException('Can only operate on a valid port handle')
  141.         
  142.         vmin = vtime = 0
  143.         
  144.         try:
  145.             (iflag, oflag, cflag, lflag, ispeed, ospeed, cc) = termios.tcgetattr(self.fd)
  146.         except termios.error:
  147.             msg = None
  148.             raise SerialException('Could not configure port: %s' % msg)
  149.  
  150.         cflag |= TERMIOS.CLOCAL | TERMIOS.CREAD
  151.         lflag &= ~(TERMIOS.ICANON | TERMIOS.ECHO | TERMIOS.ECHOE | TERMIOS.ECHOK | TERMIOS.ECHONL | TERMIOS.ECHOCTL | TERMIOS.ECHOKE | TERMIOS.ISIG | TERMIOS.IEXTEN)
  152.         oflag &= ~(TERMIOS.OPOST)
  153.         if hasattr(TERMIOS, 'IUCLC'):
  154.             iflag &= ~(TERMIOS.INLCR | TERMIOS.IGNCR | TERMIOS.ICRNL | TERMIOS.IUCLC | TERMIOS.IGNBRK)
  155.         else:
  156.             iflag &= ~(TERMIOS.INLCR | TERMIOS.IGNCR | TERMIOS.ICRNL | TERMIOS.IGNBRK)
  157.         
  158.         try:
  159.             ispeed = ospeed = baudIntToEnum[self._baudrate]
  160.         except:
  161.             raise ValueError('Invalid baud rate: %r' % self._baudrate)
  162.  
  163.         cflag &= ~(TERMIOS.CSIZE)
  164.         if self._bytesize == 8:
  165.             cflag |= TERMIOS.CS8
  166.         elif self._bytesize == 7:
  167.             cflag |= TERMIOS.CS7
  168.         elif self._bytesize == 6:
  169.             cflag |= TERMIOS.CS6
  170.         elif self._bytesize == 5:
  171.             cflag |= TERMIOS.CS5
  172.         else:
  173.             raise ValueError('Invalid char len: %r' % self._bytesize)
  174.         if self._stopbits == STOPBITS_ONE:
  175.             cflag &= ~(TERMIOS.CSTOPB)
  176.         elif self._stopbits == STOPBITS_TWO:
  177.             cflag |= TERMIOS.CSTOPB
  178.         else:
  179.             raise ValueError('Invalid stopit specification: %r' % self._stopbits)
  180.         iflag &= ~(TERMIOS.INPCK | TERMIOS.ISTRIP)
  181.         if self._parity == PARITY_NONE:
  182.             cflag &= ~(TERMIOS.PARENB | TERMIOS.PARODD)
  183.         elif self._parity == PARITY_EVEN:
  184.             cflag &= ~(TERMIOS.PARODD)
  185.             cflag |= TERMIOS.PARENB
  186.         elif self._parity == PARITY_ODD:
  187.             cflag |= TERMIOS.PARENB | TERMIOS.PARODD
  188.         else:
  189.             raise ValueError('Invalid parity: %r' % self._parity)
  190.         if hasattr(TERMIOS, 'IXANY'):
  191.             if self._xonxoff:
  192.                 iflag |= TERMIOS.IXON | TERMIOS.IXOFF | TERMIOS.IXANY
  193.             else:
  194.                 iflag &= ~(TERMIOS.IXON | TERMIOS.IXOFF | TERMIOS.IXANY)
  195.         elif self._xonxoff:
  196.             iflag |= TERMIOS.IXON | TERMIOS.IXOFF
  197.         else:
  198.             iflag &= ~(TERMIOS.IXON | TERMIOS.IXOFF)
  199.         if hasattr(TERMIOS, 'CRTSCTS'):
  200.             if self._rtscts:
  201.                 cflag |= TERMIOS.CRTSCTS
  202.             else:
  203.                 cflag &= ~(TERMIOS.CRTSCTS)
  204.         elif hasattr(TERMIOS, 'CNEW_RTSCTS'):
  205.             if self._rtscts:
  206.                 cflag |= TERMIOS.CNEW_RTSCTS
  207.             else:
  208.                 cflag &= ~(TERMIOS.CNEW_RTSCTS)
  209.         
  210.         if vmin < 0 or vmin > 255:
  211.             raise ValueError('Invalid vmin: %r ' % vmin)
  212.         
  213.         cc[TERMIOS.VMIN] = vmin
  214.         if vtime < 0 or vtime > 255:
  215.             raise ValueError('Invalid vtime: %r' % vtime)
  216.         
  217.         cc[TERMIOS.VTIME] = vtime
  218.         termios.tcsetattr(self.fd, TERMIOS.TCSANOW, [
  219.             iflag,
  220.             oflag,
  221.             cflag,
  222.             lflag,
  223.             ispeed,
  224.             ospeed,
  225.             cc])
  226.  
  227.     
  228.     def close(self):
  229.         if self._isOpen:
  230.             if self.fd:
  231.                 os.close(self.fd)
  232.                 self.fd = None
  233.             
  234.             self._isOpen = False
  235.         
  236.  
  237.     
  238.     def makeDeviceName(self, port):
  239.         return device(port)
  240.  
  241.     
  242.     def inWaiting(self):
  243.         s = fcntl.ioctl(self.fd, TIOCINQ, TIOCM_zero_str)
  244.         return struct.unpack('I', s)[0]
  245.  
  246.     
  247.     def read(self, size = 1):
  248.         if not (self.fd):
  249.             raise portNotOpenError
  250.         
  251.         read = ''
  252.         inp = None
  253.         if size > 0:
  254.             while len(read) < size:
  255.                 (ready, _, _) = select.select([
  256.                     self.fd], [], [], self.timeout)
  257.                 if not ready:
  258.                     break
  259.                 
  260.                 buf = os.read(self.fd, size - len(read))
  261.                 read = read + buf
  262.                 if self.timeout >= 0 and not buf:
  263.                     break
  264.                     continue
  265.         
  266.         return read
  267.  
  268.     
  269.     def write(self, data):
  270.         if not (self.fd):
  271.             raise portNotOpenError
  272.         
  273.         t = len(data)
  274.         d = data
  275.         while t > 0:
  276.             n = os.write(self.fd, d)
  277.             d = d[n:]
  278.             t = t - n
  279.  
  280.     
  281.     def flush(self):
  282.         self.drainOutput()
  283.  
  284.     
  285.     def flushInput(self):
  286.         if not (self.fd):
  287.             raise portNotOpenError
  288.         
  289.         termios.tcflush(self.fd, TERMIOS.TCIFLUSH)
  290.  
  291.     
  292.     def flushOutput(self):
  293.         if not (self.fd):
  294.             raise portNotOpenError
  295.         
  296.         termios.tcflush(self.fd, TERMIOS.TCOFLUSH)
  297.  
  298.     
  299.     def sendBreak(self):
  300.         if not (self.fd):
  301.             raise portNotOpenError
  302.         
  303.         termios.tcsendbreak(self.fd, 0)
  304.  
  305.     
  306.     def setRTS(self, on = 1):
  307.         if not (self.fd):
  308.             raise portNotOpenError
  309.         
  310.         if on:
  311.             fcntl.ioctl(self.fd, TIOCMBIS, TIOCM_RTS_str)
  312.         else:
  313.             fcntl.ioctl(self.fd, TIOCMBIC, TIOCM_RTS_str)
  314.  
  315.     
  316.     def setDTR(self, on = 1):
  317.         if not (self.fd):
  318.             raise portNotOpenError
  319.         
  320.         if on:
  321.             fcntl.ioctl(self.fd, TIOCMBIS, TIOCM_DTR_str)
  322.         else:
  323.             fcntl.ioctl(self.fd, TIOCMBIC, TIOCM_DTR_str)
  324.  
  325.     
  326.     def getCTS(self):
  327.         if not (self.fd):
  328.             raise portNotOpenError
  329.         
  330.         s = fcntl.ioctl(self.fd, TIOCMGET, TIOCM_zero_str)
  331.         return struct.unpack('I', s)[0] & TIOCM_CTS != 0
  332.  
  333.     
  334.     def getDSR(self):
  335.         if not (self.fd):
  336.             raise portNotOpenError
  337.         
  338.         s = fcntl.ioctl(self.fd, TIOCMGET, TIOCM_zero_str)
  339.         return struct.unpack('I', s)[0] & TIOCM_DSR != 0
  340.  
  341.     
  342.     def getRI(self):
  343.         if not (self.fd):
  344.             raise portNotOpenError
  345.         
  346.         s = fcntl.ioctl(self.fd, TIOCMGET, TIOCM_zero_str)
  347.         return struct.unpack('I', s)[0] & TIOCM_RI != 0
  348.  
  349.     
  350.     def getCD(self):
  351.         if not (self.fd):
  352.             raise portNotOpenError
  353.         
  354.         s = fcntl.ioctl(self.fd, TIOCMGET, TIOCM_zero_str)
  355.         return struct.unpack('I', s)[0] & TIOCM_CD != 0
  356.  
  357.     
  358.     def drainOutput(self):
  359.         if not (self.fd):
  360.             raise portNotOpenError
  361.         
  362.         termios.tcdrain(self.fd)
  363.  
  364.     
  365.     def nonblocking(self):
  366.         if not (self.fd):
  367.             raise portNotOpenError
  368.         
  369.         fcntl.fcntl(self.fd, FCNTL.F_SETFL, FCNTL.O_NONBLOCK)
  370.  
  371.  
  372. if __name__ == '__main__':
  373.     s = Serial(0, baudrate = 19200, bytesize = EIGHTBITS, parity = PARITY_EVEN, stopbits = STOPBITS_ONE, timeout = 3, xonxoff = 0, rtscts = 0)
  374.     s.setRTS(1)
  375.     s.setDTR(1)
  376.     s.flushInput()
  377.     s.flushOutput()
  378.     s.write('hello')
  379.     print repr(s.read(5))
  380.     print s.inWaiting()
  381.     del s
  382.  
  383.